#!/bin/sh
# http://misc.flogisoft.com/bash/tip_colors_and_formatting
# http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/the-elegant-useless-clock-prompt.html
# http://www.thegeekstuff.com/2011/01/tput-command-examples/

print_range() {
	prefix=$1
	range_low=$2
	range_high=$3
	suffix=$4

	count=$range_low
	while [ $count -le $range_high ]; do
		printf '%s%sm[%3s]%s\t' $prefix "$count" "$count" $suffix
		count=$(( $count + 1 ))
	done
	printf '\n'
}

print_cont() {
	print_range '[' '0' '7' '[0m'
	printf '\n'
}
print_ansi() {
	print_range '[' '30' '37' '[0m'
	print_range '[' '90' '97' '[0m'
	print_range '[' '40' '47' '[0m'
	print_range '[' '100' '107' '[0m'
	printf '\n'
}
print_num() {

	step=6

	low=16
	max=231

	case "$1" in
		'0')
			step=8
			low=0
			max=15
		;;
		[:1-6:])
			low=$(( 16 + ( ( $1 - 1 ) * ( $step * $step ) ) ))
			max=$(( 16 + ( $1 * ( $step * $step ) ) - 1 ))
		;;
		'7')
			low=232
			max=255
		;;
	esac

	printf '\nColors %s-%s\n\n' $low $max
	high=$(( $low + $step - 1 ))
	while [ $high -le $max ]; do
		print_range '[48;5;' "$low" "$high" '[0m'
		low=$(( $low + $step ))
		high=$(( $high + $step ))
	done
	printf '\n'
}


print_hex() {
	step=6
	low=16
	case "$1" in
		[:1-6:])
			block=$(( $1 - 1 ))
			low=$(( 16 + ( $block * $step ) ))
			inc=$(( $step * $step ))
			high=$(( $low + $step - 1 ))
			max=$(( $low + ( $step * $inc ) ))

			printf '\nColors %s-%s\n\n' $low $max
			while [ $high -le $max ]; do
				print_range '[48;5;' "$low" "$high" '[0m'
				low=$(( $low + $inc ))
				high=$(( $high + $inc ))
			done
		;;
		*)
			max=231
			high=$(( $low + $step - 1 ))

			block=0
			base=$low
			inc=$(( $step * $step ))

			printf '\nColors %s-%s\n\n' $low $max
			while [ $block -lt 6 ]; do
				while [ $high -le $max ]; do
					print_range '[48;5;' "$low" "$high" '[0m'
					low=$(( $low + $inc ))
					high=$(( $high + $inc ))
				done

				block=$(( $block + 1 ))
				base=$(( $base + $step ))
				low=$base
				high=$(( $low + $step - 1 ))
			done
		;;
	esac
	printf '\n'
}

clear_scr() {
	clear
	cat << END

                 .         .                
 :,,,             .       .             ,,,:
  Y888888bo.       :     :       .od888888Y 
   8888888888b.     :   :     .d8888888888  
   88888Y    Y8b.   .   .   .d8Y    Y88888  
  j88888  .db.  Yb. .   . .dY  .db.  88888k 
     888  Y88Y     b ( ) d     Y88Y  888    
     888b            ,.,            d888    
    j888888bd8gf     .:.     ?g8bd888888k   
       Y    .8      d: :b      8.    Y      
       :   .8  db  d : : b  db  8.   :      
          d88      8 : : 8      88b         
         d888b   .g8  .  8g.   d888b        
        :888888888Y       Y888888888:       
  ______ : 8888888 _______ 8888888 : ______ 
            8Y   Y         Y   Y8           
            Y   Terminal        Y           
            :          Colors   :           

END


}
while true
do
	clear_scr

	printf '\t1) control chars\n'
	printf '\t2) colours 30-37\n'
	printf '\t3) colours 0-15\n'
	printf '\t4) colours 0-87\n'
	printf '\t5) 16-231 pallet 1\n'
	printf '\t6) 16-231 pallet 2\n'
	printf '\t7) 232-255 grayscale\n'

	printf '\n\tq) quit\n'
	printf '  _________________________________________  \n\n'

	echo '[1-6q] '
	read com

#	printf '[1-6q] '
#	trap 'stty -raw' EXIT
#	stty raw
#	com=$(dd count=1 bs=1 2>/dev/null)
#	stty -raw

	case "$com" in
		'')
			read -n 2 inp
			echo "$inp"
			[[ "$inp" = '[A' ]] && echo yep
			[[ "$@" = '[A' ]] && read inp
			echo 'press any key '
			read inp
		;;
		'1')
			clear_scr
			print_cont
			echo 'press any key '
			read inp

		;;
		'2')
			clear_scr
			print_ansi
			echo 'press any key '
			read inp

		;;
		'3')
			clear_scr
			print_num 0
			echo 'press any key '
			read inp

		;;
		'4')
			clear_scr
			print_num 0
			print_num 1
			print_num 2
			echo 'press any key '
			read inp

		;;
		'5')
			while true
			do
				clear_scr
				print_num "$inp"
				echo '[0-7q] '
				read inp


				if [ "$inp" = 'q' ];
				then
					clear_scr
					break
				fi
			done
		;;
		'6')
			while true
			do
				clear_scr
				print_hex "$inp"
				echo '[1-6q] '
				read inp


				if [ "$inp" = 'q' ];
				then
					clear_scr
					break
				fi
			done
		;;
		'7')
			clear_scr
			print_num 7
			echo 'press any key '
			read inp

		;;
		'q')
			clear_scr
			exit
		;;
	esac
done
